package wechat

import (
	myHttp "anban/utils/http"
	"encoding/json"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
)

type WechatUserInfo struct {
	Errcode       int    `json:"errcode"`
	Errmsg        string `json:"errmsg"`
	Subscribe     int    `json:"subscribe"`
	Openid        string `json:"openid"`
	Nickname      string `json:"nickname"`
	Sex           int    `json:"sex"`
	Headimgurl    string `json:"headimgurl"`
	Unionid       string `json:"unionid"`
	SubscribeTime int64  `json:"subscribe_time"`
}

type WechatUserInfoByCode struct {
	Errcode    int    `json:"errcode"`
	Errmsg     string `json:"errmsg"`
	Openid     string `json:"openid"`
	Nickname   string `json:"nickname"`
	Sex        int    `json:"sex"`
	Headimgurl string `json:"headimgurl"`
	Unionid    string `json:"unionid"`
}

type WebAuthAccessTokenRes struct {
	Errcode      int    `json:"errcode"`
	Errmsg       string `json:"errmsg"`
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	RefreshToken string `json:"refresh_token"`
	Openid       string `json:"openid"`
	Scope        string `json:"scope"`
}

// 获取用户信息
func GetWechatUserInfo(key string, openid string) *WechatUserInfo {
	wechatUserInfo := WechatUserInfo{}
	accessToken := GetAccessToken(key)
	url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN", accessToken, openid)
	res, err := myHttp.GetData(url)
	if err != nil {
		logs.Error("request wechat user info fail:", err)
		return &wechatUserInfo
	}
	if err := json.Unmarshal([]byte(res), &wechatUserInfo); err != nil {
		logs.Error("unmarshal wechat user info response fail:", err)
		return &wechatUserInfo
	}
	if wechatUserInfo.Errcode != 0 {
		logs.Error("request wechat user info fail:", wechatUserInfo.Errmsg)
		return &wechatUserInfo
	}
	return &wechatUserInfo
}

// 网页授权获取微信用户信息
func GetWechatUserInfoByCode(code string) *WechatUserInfoByCode {
	appId := beego.AppConfig.String("wechatappid")
	appSecret := beego.AppConfig.String("wechatappsecret")
	url := fmt.Sprintf(
		"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
		appId, appSecret, code)
	res, err := myHttp.GetData(url)
	if err != nil {
		logs.Error("request user info by code fail:", err)
		return nil
	}
	var data WebAuthAccessTokenRes
	err = json.Unmarshal([]byte(res), &data)
	if err != nil {
		logs.Error("unmarshal get user info by code response fail:", err)
		return nil
	}
	if data.Errcode != 0 {
		logs.Error("request user info by code fail:", data.Errmsg)
		return nil
	}

	url = fmt.Sprintf(
		"https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s&lang=zh_CN",
		data.AccessToken, data.Openid)
	res, err = myHttp.GetData(url)
	if err != nil {
		logs.Error("request user info by code fail:", err)
		return nil
	}
	var userInfo WechatUserInfoByCode
	err = json.Unmarshal([]byte(res), &userInfo)
	if err != nil {
		logs.Error("unmarshal get user info by code response fail:", err)
		return nil
	}
	if userInfo.Errcode != 0 {
		logs.Error("request user info by code fail:", userInfo.Errmsg)
		return nil
	}
	return &userInfo
}
